#include "media_service.h"
#include "utils_log.h"
#include <sstream>

namespace El {
namespace Onvif {

MediaService::MediaService() {
    EL_INFO("MediaService initialized");
}

std::string MediaService::HandleRequest(const SoapMessage& request, const std::string& /*remoteAddr*/) {
    EL_DEBUG("MediaService handling request: " + request.action);
    
    if (request.action == "GetProfiles") {
        return HandleGetProfiles(request);
    } else if (request.action == "GetStreamUri") {
        return HandleGetStreamUri(request);
    } else if (request.action == "GetSnapshotUri") {
        return HandleGetSnapshotUri(request);
    } else if (request.action == "GetVideoSources") {
        return HandleGetVideoSources(request);
    }
    
    SoapMessageParser parser;
    return parser.GenerateSoapFault(SoapError::NotSupported, "Media action not supported: " + request.action);
}

std::string MediaService::HandleGetProfiles(const SoapMessage& /*message*/) {
    std::ostringstream response;
    response << "    <trt:GetProfilesResponse>\n";
    
    response << "      <trt:Profiles token=\"Profile_1\" fixed=\"true\">\n"
             << "        <tt:Name>Profile_1</tt:Name>\n"
             << "        <tt:VideoSourceConfiguration token=\"VideoSourceConfig_1\">\n"
             << "          <tt:Name>VideoSourceConfig_1</tt:Name>\n"
             << "          <tt:UseCount>1</tt:UseCount>\n"
             << "          <tt:SourceToken>VideoSource_1</tt:SourceToken>\n"
             << "          <tt:Bounds x=\"0\" y=\"0\" width=\"1920\" height=\"1080\"/>\n"
             << "        </tt:VideoSourceConfiguration>\n"
             << "        <tt:VideoEncoderConfiguration token=\"VideoEncoderConfig_1\">\n"
             << "          <tt:Name>VideoEncoderConfig_1</tt:Name>\n"
             << "          <tt:UseCount>1</tt:UseCount>\n"
             << "          <tt:Encoding>H264</tt:Encoding>\n"
             << "          <tt:Resolution>\n"
             << "            <tt:Width>1920</tt:Width>\n"
             << "            <tt:Height>1080</tt:Height>\n"
             << "          </tt:Resolution>\n"
             << "          <tt:Quality>5</tt:Quality>\n"
             << "          <tt:RateControl>\n"
             << "            <tt:FrameRateLimit>25</tt:FrameRateLimit>\n"
             << "            <tt:EncodingInterval>1</tt:EncodingInterval>\n"
             << "            <tt:BitrateLimit>2048</tt:BitrateLimit>\n"
             << "          </tt:RateControl>\n"
             << "        </tt:VideoEncoderConfiguration>\n"
             << "      </trt:Profiles>\n";
    
    response << "    </trt:GetProfilesResponse>\n";
    
    SoapMessageParser parser;
    return parser.GenerateSoapResponse(response.str(), "GetProfilesResponse");
}

std::string MediaService::HandleGetStreamUri(const SoapMessage& /*message*/) {
    std::ostringstream response;
    response << "    <trt:GetStreamUriResponse>\n"
             << "      <trt:MediaUri>\n"
             << "        <tt:Uri>rtsp://192.168.1.100:554/stream1</tt:Uri>\n"
             << "        <tt:InvalidAfterConnect>false</tt:InvalidAfterConnect>\n"
             << "        <tt:InvalidAfterReboot>false</tt:InvalidAfterReboot>\n"
             << "        <tt:Timeout>PT60S</tt:Timeout>\n"
             << "      </trt:MediaUri>\n"
             << "    </trt:GetStreamUriResponse>\n";
    
    SoapMessageParser parser;
    return parser.GenerateSoapResponse(response.str(), "GetStreamUriResponse");
}

std::string MediaService::HandleGetSnapshotUri(const SoapMessage& /*message*/) {
    std::ostringstream response;
    response << "    <trt:GetSnapshotUriResponse>\n"
             << "      <trt:MediaUri>\n"
             << "        <tt:Uri>http://192.168.1.100:8080/snapshot</tt:Uri>\n"
             << "        <tt:InvalidAfterConnect>false</tt:InvalidAfterConnect>\n"
             << "        <tt:InvalidAfterReboot>false</tt:InvalidAfterReboot>\n"
             << "        <tt:Timeout>PT60S</tt:Timeout>\n"
             << "      </trt:MediaUri>\n"
             << "    </trt:GetSnapshotUriResponse>\n";
    
    SoapMessageParser parser;
    return parser.GenerateSoapResponse(response.str(), "GetSnapshotUriResponse");
}

std::string MediaService::HandleGetVideoSources(const SoapMessage& /*message*/) {
    std::ostringstream response;
    response << "    <trt:GetVideoSourcesResponse>\n"
             << "      <trt:VideoSources token=\"VideoSource_1\">\n"
             << "        <tt:Framerate>25</tt:Framerate>\n"
             << "        <tt:Resolution>\n"
             << "          <tt:Width>1920</tt:Width>\n"
             << "          <tt:Height>1080</tt:Height>\n"
             << "        </tt:Resolution>\n"
             << "        <tt:Imaging>\n"
             << "          <tt:Brightness>50</tt:Brightness>\n"
             << "          <tt:ColorSaturation>50</tt:ColorSaturation>\n"
             << "          <tt:Contrast>50</tt:Contrast>\n"
             << "          <tt:Sharpness>50</tt:Sharpness>\n"
             << "        </tt:Imaging>\n"
             << "      </trt:VideoSources>\n"
             << "    </trt:GetVideoSourcesResponse>\n";
    
    SoapMessageParser parser;
    return parser.GenerateSoapResponse(response.str(), "GetVideoSourcesResponse");
}

std::string MediaService::EscapeXml(const std::string& input) const {
    std::string result = input;
    
    size_t pos = 0;
    while ((pos = result.find('&', pos)) != std::string::npos) {
        result.replace(pos, 1, "&amp;");
        pos += 5;
    }
    
    pos = 0;
    while ((pos = result.find('<', pos)) != std::string::npos) {
        result.replace(pos, 1, "&lt;");
        pos += 4;
    }
    
    pos = 0;
    while ((pos = result.find('>', pos)) != std::string::npos) {
        result.replace(pos, 1, "&gt;");
        pos += 4;
    }
    
    pos = 0;
    while ((pos = result.find('"', pos)) != std::string::npos) {
        result.replace(pos, 1, "&quot;");
        pos += 6;
    }
    
    return result;
}

} // namespace Onvif
} // namespace El